package com.java.imauoj.controler;

import com.java.imauoj.moudel.Classes;
import com.java.imauoj.moudel.ResultVo;
import com.java.imauoj.moudel.Role;
import com.java.imauoj.moudel.User;
import com.java.imauoj.sercive.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("api/v1")
public class ClassControler {

    @Autowired
    private ClassService service;

    @PostMapping("getClassList")
    public ResultVo getClassList(HttpSession session) {

        ResultVo re = new ResultVo();
        User userLogin = (User) session.getAttribute("login");
        if (userLogin != null) {
            Role role = (Role) session.getAttribute("role");
            if (role.getIs_get_class() == 0) {
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(0);
                re.setMessage("获取成功");
                re.setData(service.getClassList());
            }
        }
        return re;
    }

    //班级的操作
    @PostMapping(value = "classOperate")
    public ResultVo userOperate(@RequestParam(value = "active") int active, Classes cla, HttpSession session){

        ResultVo re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_opt_class() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                if(active == 0){
                    Classes classes = service.updateClass(cla);
                    re.setMessage("修改成功");
                    re.setData(classes);
                }else if(active == 1){
                    Classes classes = service.addClass(cla);
                    re.setMessage("添加成功");
                    re.setData(classes);
                }else if(active == -1){
                    Classes classes = service.deleteClass(cla.getClass_id());
                    re.setMessage("删除成功");
                    re.setData(classes);
                }
            }
        }
        return re;
    }
}
